﻿using System;
using System.Collections.Generic;
using System.Text;
using WinApp;

namespace SolutionTests
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileuri = "http://server969.at.youporn.com/e4/flv/428969_Horny_Milf_Banged_On_Cam.flv?e=1274040179&h=fa37e5e8fd874968b4526f7ea01a5a5c";
            string savepat = "D:\\" + fileuri.Substring(fileuri.LastIndexOf("/") +1);
            FileDownloader fdp = new FileDownloader(fileuri, savepat);
            DownloadFileTest(fdp);
            Console.ReadLine();
        
        
        
        }

        static void DownloadFileTest(FileDownloader fdp)
        {
            
            FileDownloader fd = fdp;
            string fileuri = "http://server969.at.youporn.com/e4/flv/428969_Horny_Milf_Banged_On_Cam.flv?e=1274040179&h=fa37e5e8fd874968b4526f7ea01a5a5c";
            Console.WriteLine("Start download file: {0}",fileuri.Substring(fileuri.LastIndexOf("/")));
            
            fd.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(fd_DownloadProgressChanged);
            fd.DownloadFileCompleted +=new System.ComponentModel.AsyncCompletedEventHandler(fd_DownloadFileCompleted);
            
            fd.StartDownload();
        }

        static void fd_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Console.WriteLine("Error: {0}", e.Error.Message);
            }
            else
            {
                Console.WriteLine("Download feito com sucesso!");
            }
        }

        static void fd_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            DateTime dt = (DateTime)e.UserState;

            if (e.BytesReceived > 0 && e.TotalBytesToReceive > 0)
            {
                Console.WriteLine("Bytes Received: {0:N2} | Percentage: {1:000} | Total Size: {2:N2} | Average: {3:N2} Kbps", e.BytesReceived / 1024d / 1024d, e.ProgressPercentage, e.TotalBytesToReceive / 1024d / 1024d, (e.TotalBytesToReceive / (DateTime.Now - dt).Seconds) / 1024d); 
            }
            
        }
    }
}
